/**
 * jims
 */
package com.jims.master.modules.operation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.modules.operation.entity.*;
import com.jims.master.modules.operation.service.*;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.service.OrgDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;

import java.util.List;

/**
 * 手术安排确认Controller
 * @author DT
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/operation/operationScheduleAudit")
public class OperationScheduleAuditController extends BaseController {
    @Autowired
    private OrgDeptService orgDeptService;
    @Autowired
    private OperationScheduleService operationScheduleService;
    @Autowired
    private OperationApplyService operationApplyService;
    @Autowired
    private OperationStaffBeforeService operationStaffBeforeService;
    @ModelAttribute
    public OperationSchedule get(@RequestParam(required=false) String id) {
        OperationSchedule entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = operationScheduleService.get(id);
        }
        if (entity == null){
            entity = new OperationSchedule();
        }
        return entity;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(OperationSchedule operationSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<OperationSchedule> page = operationScheduleService.findPage(new Page<OperationSchedule>(request, response), operationSchedule);
        model.addAttribute("page", page);
        OrgDept orgDept = new OrgDept();
        Page<OrgDept> orgDeptList = orgDeptService.findPage(new Page<OrgDept>(request, response), orgDept);
        model.addAttribute("orgDeptList", orgDeptList);
        return "modules/operation/operationScheduleAuditIndex";
    }

    /**
     * 加载右侧form表
     * @param operationSchedule
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"auditForm", ""})
    public String auditForm(OperationSchedule operationSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (operationSchedule != null) {
            if (StringUtils.isNotBlank(operationSchedule.getId())) {
                operationSchedule = operationScheduleService.get(operationSchedule);
                OperationStaffBefore operationStaffBefore = new OperationStaffBefore();
                operationStaffBefore.setScheduleId(operationSchedule.getId());
                OperationStaffBefore zdStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"0");
                OperationStaffBefore myStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"1");
                OperationStaffBefore sxStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"2");
                OperationStaffBefore mzStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"3");
                OperationStaffBefore yzStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"4");
                OperationStaffBefore gyStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"5");
                OperationStaffBefore stStaffBefore=operationScheduleService.getStaff(operationStaffBefore,"6");
                operationSchedule.setZdStaffBefore(zdStaffBefore);
                operationSchedule.setMyStaffBefore(myStaffBefore);
                operationSchedule.setSxStaffBefore(sxStaffBefore);
                operationSchedule.setMzStaffBefore(mzStaffBefore);
                operationSchedule.setYzStaffBefore(yzStaffBefore);
                operationSchedule.setGyStaffBefore(gyStaffBefore);
                operationSchedule.setStStaffBefore(stStaffBefore);
            }
        }
        model.addAttribute("operationSchedule", operationSchedule);
        return "modules/operation/operationScheduleAuditForm";
    }

    /**
     * 刷新左侧列表
     * @param operationSchedule
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"auditList", ""})
    public String auditList(OperationSchedule operationSchedule, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<OperationSchedule> page = operationScheduleService.findPage(new Page<OperationSchedule>(request, response), operationSchedule);
        model.addAttribute("page", page);
        return "modules/operation/operationScheduleAuditView";
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(OperationSchedule operationSchedule, Model model) {
        StringData data=new StringData();
        int i = operationScheduleService.saveOperationSchedule(operationSchedule);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(OperationSchedule operationSchedule) {
        StringData data=new StringData();
        int i=operationScheduleService.deleteAll(operationSchedule);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
        return data;
    }
    /**
     * 手术安排打印
     * @param operationSchedule
     * @return com.jims.common.data.StringData
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"operationSchedulePrint", ""})
    @ResponseBody
    public StringData operationSchedulePrint(OperationSchedule operationSchedule) {
        StringData stringData = new StringData();
        String printPath = operationScheduleService.operationSchedulePrint(operationSchedule);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;

    }

}